home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / edit / thesrc20.zip / colour.c < prev    next >
C/C++ Source or Header  |  1995-01-26  |  16KB  |  426 lines

  1. /***********************************************************************/
  2. /* COLOUR.C - Colour related functions                                 */
  3. /* This file contains all commands that can be assigned to function    */
  4. /* keys or typed on the command line.                                  */
  5. /***********************************************************************/
  6. /*
  7.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  8.  * Copyright (C) 1991-1995 Mark Hessling
  9.  *
  10.  * This program is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License as
  12.  * published by the Free Software Foundation; either version 2 of
  13.  * the License, or any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18.  * General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to:
  22.  *
  23.  *    The Free Software Foundation, Inc.
  24.  *    675 Mass Ave,
  25.  *    Cambridge, MA 02139 USA.
  26.  *
  27.  *
  28.  * If you make modifications to this software that you feel increases
  29.  * it usefulness for the rest of the community, please email the
  30.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  31.  * This software is going to be maintained and enhanced as deemed
  32.  * necessary by the community.
  33.  *
  34.  * Mark Hessling                     email: M.Hessling@gu.edu.au
  35.  * 36 David Road                     Phone: +61 7 849 7731
  36.  * Holland Park                      Fax:   +61 7 875 5314
  37.  * QLD 4121
  38.  * Australia
  39.  */
  40.  
  41. /*
  42. $Id: colour.c 2.0 1995/01/26 16:29:47 MH Release MH $
  43. */
  44.  
  45. #include "the.h"
  46. #include "proto.h"
  47.  
  48. /*                        attributes                                   */
  49. /*             FILEAREA CURLINE  BLOCK    CBLOCK                       */
  50. /*             CMDLINE  IDLINE   MSGLINE  ARROW                        */
  51. /*             PREFIX   PENDING  SCALE    TOFEOF                       */
  52. /*             CTOFEOF  TABLINE  SHADOW   STATAREA                     */
  53. /*             DIVIDER  RESERVED                                       */
  54.  
  55. #ifdef A_COLOR
  56.  static chtype the_fore[ATTR_MAX] =
  57.               {COLOR_WHITE,COLOR_WHITE,COLOR_BLUE,COLOR_RED,
  58.                COLOR_BLACK,COLOR_BLUE,COLOR_RED,COLOR_BLACK,
  59.                COLOR_BLACK,COLOR_RED,COLOR_YELLOW,COLOR_WHITE,
  60.                COLOR_WHITE,COLOR_YELLOW,COLOR_RED,COLOR_BLUE,
  61.                COLOR_RED,COLOR_WHITE};
  62.  static chtype the_back[ATTR_MAX] =
  63.               {COLOR_BLUE,COLOR_BLUE,COLOR_WHITE,COLOR_WHITE,
  64.                COLOR_CYAN,COLOR_WHITE,COLOR_WHITE,COLOR_CYAN,
  65.                COLOR_CYAN,COLOR_WHITE,COLOR_BLUE,COLOR_BLUE,
  66.                COLOR_BLUE,COLOR_BLUE,COLOR_WHITE,COLOR_WHITE,
  67.                COLOR_WHITE,COLOR_BLACK};
  68.  static chtype the_mod[ATTR_MAX] =
  69.               {A_NORMAL,A_BOLD,  A_NORMAL,A_NORMAL,
  70.                A_NORMAL,A_NORMAL,A_NORMAL,A_NORMAL,
  71.                A_NORMAL,A_NORMAL,A_BOLD,  A_BOLD,
  72.                A_BOLD,  A_BOLD,  A_NORMAL,A_NORMAL,
  73.                A_NORMAL,A_NORMAL};
  74.  static chtype kedit_fore[ATTR_MAX] =
  75.               {COLOR_CYAN,COLOR_YELLOW,COLOR_CYAN,COLOR_YELLOW,
  76.                COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,
  77.                COLOR_YELLOW,COLOR_WHITE,COLOR_YELLOW,COLOR_CYAN,
  78.                COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,
  79.                COLOR_CYAN,COLOR_YELLOW};
  80.  static chtype kedit_back[ATTR_MAX] =
  81.               {COLOR_BLUE,COLOR_BLUE,COLOR_WHITE,COLOR_WHITE,
  82.                COLOR_BLUE,COLOR_BLUE,COLOR_BLUE,COLOR_BLUE,
  83.                COLOR_BLUE,COLOR_BLUE,COLOR_BLUE,COLOR_BLUE,
  84.                COLOR_BLUE,COLOR_BLUE,COLOR_BLUE,COLOR_BLUE,
  85.                COLOR_BLUE,COLOR_BLUE};
  86.  static chtype kedit_mod[ATTR_MAX]  =
  87.               {A_BOLD,  A_BOLD,  A_BOLD,  A_BOLD,
  88.                A_BOLD,  A_BOLD,  A_BOLD,  A_BOLD,
  89.                A_NORMAL,A_BOLD,  A_BOLD,  A_BOLD,
  90.                A_BOLD,  A_BOLD,  A_NORMAL,A_BOLD,
  91.                A_BOLD,  A_BOLD};
  92.  static chtype xedit_fore[ATTR_MAX] =
  93.               {COLOR_GREEN, COLOR_CYAN, COLOR_BLACK,COLOR_CYAN,
  94.                COLOR_YELLOW,COLOR_GREEN,COLOR_RED,  COLOR_GREEN,
  95.                COLOR_GREEN, COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,
  96.                COLOR_GREEN, COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,
  97.                COLOR_GREEN, COLOR_GREEN};
  98.  static chtype xedit_back[ATTR_MAX] =
  99.               {COLOR_BLACK,COLOR_BLACK,COLOR_GREEN,COLOR_GREEN,
  100.                COLOR_BLACK,COLOR_BLACK,COLOR_BLACK,COLOR_BLACK,
  101.                COLOR_BLACK,COLOR_BLACK,COLOR_BLACK,COLOR_BLACK,
  102.                COLOR_BLACK,COLOR_BLACK,COLOR_BLACK,COLOR_BLACK,
  103.                COLOR_BLACK,COLOR_BLACK};
  104.  static chtype xedit_mod[ATTR_MAX]  =
  105.               {A_NORMAL,A_BOLD,  A_NORMAL,A_BOLD,
  106.                A_NORMAL,A_BOLD,  A_BOLD,  A_BOLD,
  107.                A_NORMAL,A_BOLD,  A_BOLD,  A_NORMAL,
  108.                A_BOLD,  A_BOLD,  A_NORMAL,A_BOLD,
  109.                A_BOLD,  A_BOLD};
  110. #else
  111.  static chtype the_fore[ATTR_MAX]   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  112.  static chtype the_back[ATTR_MAX]   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  113.  static chtype the_mod[ATTR_MAX]    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  114.  static chtype kedit_fore[ATTR_MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  115.  static chtype kedit_back[ATTR_MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  116.  static chtype kedit_mod[ATTR_MAX]  = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  117.  static chtype xedit_fore[ATTR_MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  118.  static chtype xedit_back[ATTR_MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  119.  static chtype xedit_mod[ATTR_MAX]  = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  120. #endif
  121.  static chtype the_mono[ATTR_MAX] =
  122.               {A_NORMAL,A_BOLD,A_REVERSE,A_BOLD|A_REVERSE,
  123.                A_BOLD,A_REVERSE,A_BLINK,A_BOLD,
  124.                A_REVERSE,A_BOLD|A_REVERSE,A_BOLD,A_BOLD,
  125.                A_BOLD,A_BOLD,A_BOLD,A_REVERSE,
  126.                A_BOLD,A_BOLD};
  127.  static chtype kedit_mono[ATTR_MAX] =
  128.               {A_NORMAL,A_BOLD,  A_REVERSE,A_REVERSE|A_BOLD,
  129.                A_NORMAL,A_NORMAL,A_BOLD,  A_BOLD,
  130.                A_NORMAL,A_BOLD,  A_NORMAL,A_NORMAL,
  131.                A_BOLD,  A_NORMAL,A_NORMAL,A_BOLD,
  132.                A_NORMAL,A_NORMAL};
  133.  static chtype xedit_mono[ATTR_MAX] =
  134.               {A_NORMAL,A_BOLD,  A_REVERSE,A_BOLD|A_REVERSE,
  135.                A_NORMAL,A_BOLD,  A_BOLD,  A_BOLD,
  136.                A_NORMAL,A_BOLD,  A_BOLD,  A_NORMAL,
  137.                A_BOLD,  A_BOLD,  A_NORMAL,A_BOLD,
  138.                A_BOLD,  A_BOLD};
  139.  
  140. /***********************************************************************/
  141. #ifdef PROTO
  142. short parse_colours(CHARTYPE *attrib,COLOUR_ATTR *pattr,CHARTYPE **rem,bool spare)
  143. #else
  144. short parse_colours(attrib,pattr,rem,spare)
  145. CHARTYPE *attrib;
  146. COLOUR_ATTR *pattr;
  147. CHARTYPE **rem;
  148. bool spare;
  149. #endif
  150. /***********************************************************************/
  151. {
  152. /*------------------------- external data -----------------------------*/
  153. extern bool colour_support;
  154. /*--------------------------- local data ------------------------------*/
  155.  struct attributes
  156.  {
  157.   CHARTYPE *attrib;
  158.   short attrib_min_len;
  159.   chtype actual_attrib;
  160.   chtype colour_modifier;
  161.   bool attrib_modifier;
  162.   bool attrib_allowed_on_mono;
  163.  };
  164.  typedef struct attributes ATTRIBS;
  165. #define NO_ATTRIBS 21
  166.  static ATTRIBS valid_attribs[NO_ATTRIBS] =
  167.  {
  168.   {(CHARTYPE *)"black",3,COLOR_BLACK,0,FALSE,FALSE},
  169.   {(CHARTYPE *)"gray",3,COLOR_BLACK,A_BOLD,FALSE,FALSE},
  170.   {(CHARTYPE *)"grey",3,COLOR_BLACK,A_BOLD,FALSE,FALSE},
  171.   {(CHARTYPE *)"blue",3,COLOR_BLUE,0,FALSE,FALSE},
  172.   {(CHARTYPE *)"green",1,COLOR_GREEN,0,FALSE,FALSE},
  173.   {(CHARTYPE *)"cyan",1,COLOR_CYAN,0,FALSE,FALSE},
  174.   {(CHARTYPE *)"red",3,COLOR_RED,0,FALSE,FALSE},
  175.   {(CHARTYPE *)"magenta",1,COLOR_MAGENTA,0,FALSE,FALSE},
  176.   {(CHARTYPE *)"yellow",1,COLOR_YELLOW,0,FALSE,FALSE},
  177.   {(CHARTYPE *)"brown",1,COLOR_YELLOW,0,FALSE,FALSE},
  178.   {(CHARTYPE *)"white",1,COLOR_WHITE,0,FALSE,FALSE},
  179.   {(CHARTYPE *)"turquoise",1,COLOR_CYAN,0,FALSE,FALSE},
  180.   {(CHARTYPE *)"pink",1,COLOR_MAGENTA,0,FALSE,FALSE},
  181.   {(CHARTYPE *)"normal",3,A_NORMAL,0,TRUE,TRUE},
  182.   {(CHARTYPE *)"blink",3,A_BLINK,0,TRUE,TRUE},
  183.   {(CHARTYPE *)"bold",2,A_BOLD,0,TRUE,TRUE},
  184.   {(CHARTYPE *)"bright",3,A_BOLD,0,TRUE,TRUE},
  185.   {(CHARTYPE *)"high",1,A_BOLD,0,TRUE,TRUE},
  186.   {(CHARTYPE *)"reverse",3,A_REVERSE,0,TRUE,TRUE},
  187.   {(CHARTYPE *)"underline",1,A_UNDERLINE,0,TRUE,TRUE},
  188.   {(CHARTYPE *)",",1,8,0,FALSE,TRUE},
  189.  };
  190.  register short i=0;
  191.  short num_colours=0;
  192.  chtype pairnum=pattr->pair;
  193.  chtype mod=pattr->mod;
  194.  chtype mono=pattr->mono;
  195.  chtype fg=FOREFROMPAIR(pattr->pair);
  196.  chtype bg=BACKFROMPAIR(pattr->pair);
  197.  CHARTYPE *string=NULL;
  198.  CHARTYPE *p=NULL;
  199.  bool found=FALSE;
  200.  bool reverse=FALSE;
  201.  bool spare_pos=FALSE;
  202.  int offset=0;
  203. /*--------------------------- processing ------------------------------*/
  204. #ifdef TRACE
  205.  trace_function("commutil.c:parse_colours");
  206. #endif
  207. /*---------------------------------------------------------------------*/
  208. /* Get a copy of the passed string and wreck it rather than the passed */
  209. /* string.                                                             */
  210. /*---------------------------------------------------------------------*/
  211.  if ((string = (CHARTYPE *)my_strdup(attrib)) == NULL)
  212.    {
  213.     display_error(30,(CHARTYPE *)"",FALSE);
  214. #ifdef TRACE
  215.     trace_return();
  216. #endif
  217.     return(RC_OUT_OF_MEMORY);
  218.    }
  219.  
  220.  p = (CHARTYPE *)strtok(string," \b");
  221.  while(p != NULL)
  222.    {
  223.     found = FALSE;
  224.     for (i=0;i<NO_ATTRIBS;i++)
  225.        {
  226.         if (equal(valid_attribs[i].attrib,p,valid_attribs[i].attrib_min_len))
  227.           {
  228.            found = TRUE;
  229.            if (!valid_attribs[i].attrib_allowed_on_mono
  230.            &&  !colour_support)
  231.              {
  232.               display_error(61,(CHARTYPE *)p,FALSE);
  233.               (*the_free)(string);
  234. #ifdef TRACE
  235.               trace_return();
  236. #endif
  237.               return(RC_INVALID_OPERAND);
  238.              }
  239.            if (valid_attribs[i].attrib_modifier)
  240.              {
  241.               if (colour_support)
  242.                 {
  243.                  if (valid_attribs[i].actual_attrib == A_REVERSE)
  244.                     reverse = TRUE;
  245.                  else
  246.                     mod = (valid_attribs[i].actual_attrib==A_NORMAL)?A_NORMAL:mod | valid_attribs[i].actual_attrib;
  247.                 }
  248.               else
  249.                  mono = (valid_attribs[i].actual_attrib==A_NORMAL)?A_NORMAL:mono | valid_attribs[i].actual_attrib;
  250.               offset += strlen(p) + 1;
  251.               break;
  252.              }
  253.            else
  254.              {
  255.               switch(num_colours)
  256.                 {
  257.                  case 0:
  258.                       if (valid_attribs[i].actual_attrib != 8)
  259.                         {
  260.                          fg = valid_attribs[i].actual_attrib;
  261.                          mod |= valid_attribs[i].colour_modifier;
  262.                         } else ;
  263. /*
  264.                       fg = valid_attribs[i].actual_attrib;
  265.                       mod |= valid_attribs[i].colour_modifier;
  266. */
  267.                       num_colours++;
  268.                       offset += strlen(p) + 1;
  269.                       break;
  270.                  case 1:
  271.                       if (valid_attribs[i].actual_attrib != 8)
  272.                         {
  273.                          bg = valid_attribs[i].actual_attrib;
  274.                          mod |= valid_attribs[i].colour_modifier;
  275.                         } else ;
  276. /*
  277.                       bg = valid_attribs[i].actual_attrib;
  278.                       mod |= valid_attribs[i].colour_modifier;
  279. */
  280.                       num_colours++;
  281.                       offset += strlen(p) + 1;
  282.                       break;
  283.                  default:
  284.                       if (spare)
  285.                         {
  286.                          spare_pos = TRUE;
  287.                          *rem = (CHARTYPE *)strstr(attrib+offset,p);
  288.                          break;
  289.                         }
  290.                       display_error(1,(CHARTYPE *)p,FALSE);
  291.                       (*the_free)(string);
  292. #ifdef TRACE
  293.                       trace_return();
  294. #endif
  295.                       return(RC_INVALID_OPERAND);
  296.                       break;
  297.                 }
  298.               if (spare_pos)
  299.                  break;
  300.              }
  301.            break;
  302.           }
  303.        }
  304.     if (spare_pos && found)
  305.        break;
  306.     if (!found)
  307.       {
  308.        if (spare)
  309.          {
  310.           *rem = (CHARTYPE *)strstr(attrib+offset,p);
  311.           break;
  312.          }
  313.        display_error(1,(CHARTYPE *)p,FALSE);
  314.        (*the_free)(string);
  315. #ifdef TRACE
  316.        trace_return();
  317. #endif
  318.        return(RC_INVALID_OPERAND);
  319.       }
  320.     p = (CHARTYPE *)strtok(NULL," ");
  321.    }
  322.  pattr->pair = (reverse) ? ATTR2PAIR(bg,fg) : ATTR2PAIR(fg,bg);
  323.  pattr->mod = mod;
  324.  pattr->mono = mono;
  325.  (*the_free)(string);
  326. #ifdef TRACE
  327.  trace_return();
  328. #endif
  329.  return(RC_OK);
  330. }
  331.  
  332. /***********************************************************************/
  333. #ifdef PROTO
  334. chtype set_colour(COLOUR_ATTR *attr)
  335. #else
  336. chtype set_colour(attr)
  337. COLOUR_ATTR *attr;
  338. #endif
  339. /***********************************************************************/
  340. {
  341. /*------------------------- external data -----------------------------*/
  342.  extern bool colour_support;
  343. /*--------------------------- local data ------------------------------*/
  344.  chtype color=0;
  345. /*--------------------------- processing ------------------------------*/
  346. #ifdef TRACE
  347.  trace_function("commutil.c:set_colour");
  348. #endif
  349.  
  350.  color = attr->mono;
  351. #ifdef A_COLOR
  352.  if (colour_support)
  353.     color = (attr->pair) ? COLOR_PAIR(attr->pair) | attr->mod : attr->mod;
  354. #endif
  355.  
  356. #if 0
  357.  color = fd->mono[area];
  358. #ifdef A_COLOR
  359.  if (colour_support)
  360.    {
  361.     init_pair(area+1,fd->fg[area],fd->bg[area]);
  362.     color = COLOR_PAIR(area+1) | fd->mod[area];
  363.    }
  364. #endif
  365. #endif
  366.  
  367. #ifdef TRACE
  368.  trace_return();
  369. #endif
  370.  return(color);
  371. }
  372. /***********************************************************************/
  373. #ifdef PROTO
  374. void set_up_default_colours(FILE_DETAILS *fd)
  375. #else
  376. void set_up_default_colours(fd)
  377. FILE_DETAILS *fd;
  378. #endif
  379. /***********************************************************************/
  380. /* This function is called as part of reading in a new file.           */
  381. /***********************************************************************/
  382. {
  383. /*------------------------- external data -----------------------------*/
  384.  extern short compatible;
  385. /*--------------------------- local data ------------------------------*/
  386.  register short i=0;
  387. /*--------------------------- processing ------------------------------*/
  388. #ifdef TRACE
  389.  trace_function("default.c: set_up_default_colours");
  390. #endif
  391. /*---------------------------------------------------------------------*/
  392. /* Set up default colours.                                             */
  393. /*---------------------------------------------------------------------*/
  394.  switch(compatible)
  395.    {
  396.     case COMPAT_THE:
  397.          for (i=0;i<ATTR_MAX;i++)
  398.            {
  399.             fd->attr[i].pair = ATTR2PAIR(the_fore[i],the_back[i]);
  400.             fd->attr[i].mod = the_mod[i];
  401.             fd->attr[i].mono = the_mono[i];
  402.            }
  403.          break;
  404.     case COMPAT_XEDIT:
  405.          for (i=0;i<ATTR_MAX;i++)
  406.            {
  407.             fd->attr[i].pair = ATTR2PAIR(xedit_fore[i],xedit_back[i]);
  408.             fd->attr[i].mod = xedit_mod[i];
  409.             fd->attr[i].mono = xedit_mono[i];
  410.            }
  411.          break;
  412.     case COMPAT_KEDIT:
  413.          for (i=0;i<ATTR_MAX;i++)
  414.            {
  415.             fd->attr[i].pair = ATTR2PAIR(kedit_fore[i],kedit_back[i]);
  416.             fd->attr[i].mod = kedit_mod[i];
  417.             fd->attr[i].mono = kedit_mono[i];
  418.            }
  419.          break;
  420.    }
  421. #ifdef TRACE
  422.  trace_return();
  423. #endif
  424.  return;
  425. }
  426.